//
//  SinaComposePictureController.m
//  MySinaWeiboProjectForOC
//
//  Created by apple on 2016/11/28.
//  Copyright © 2016年 WangChen. All rights reserved.
//

#import "SinaComposePictureController.h"
#import "SinaComposePictureCell.h"

@interface SinaComposePictureController ()<pictureCellDelegate>

@end

//最大图片数
static NSInteger maxPictureCount = 3;

@implementation SinaComposePictureController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    [self.collectionView registerClass:[SinaComposePictureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    NSInteger delta = self.imageArray.count == maxPictureCount ? 0 : 1;
    return _imageArray.count + delta;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SinaComposePictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.delegate = self;
    
    if(indexPath.item < self.imageArray.count){
        UIImage *btnImage = self.imageArray[indexPath.item];
        cell.image = btnImage;
    }else{
        cell.image = nil;
    }
    
    
    return cell;
}



#pragma mark
#pragma mark - pictureCell的代理方法
#pragma mark
#pragma mark - 点击了删除图片按钮
-(void)closeBtnDidClick:(SinaComposePictureCell *)cell{
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    [self.imageArray removeObjectAtIndex:indexPath.item];
    [self.collectionView reloadData];
}



#pragma mark
#pragma mark - 点击了图片按钮
-(void)addBtndidClick:(SinaComposePictureCell *)cell{
    self.btnDidClick();
}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
